package com.hkts.naturalgas.base.service.serviceimpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.naturalgas.base.entity.CalcinationDesulfurizationDenitration;
import com.hkts.naturalgas.base.entity.CalcinationPurificationBleaching;
import com.hkts.naturalgas.base.entity.HktsTailGas;
import com.hkts.naturalgas.base.entity.RoastingPurifyWetElectricity;
import com.hkts.naturalgas.base.mapper.CalcinationDesulfurizationDenitrationMapper;
import com.hkts.naturalgas.base.mapper.HktsTailGasMapper;
import com.hkts.naturalgas.base.mapper.HktsWasteGasMapper;
import com.hkts.naturalgas.base.service.ICalcinationDesulfurizationDenitrationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.naturalgas.model.ResultData;
import com.hkts.naturalgas.utils.DataHandlerUtils;
import com.hkts.naturalgas.utils.ExcelPortUtil;
import com.hkts.naturalgas.utils.resultMapUtils;
import com.hkts.naturalgas.base.service.IHktsTailGasService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-05-31
 */
@Service
@Transactional
public class CalcinationDesulfurizationDenitrationServiceImpl extends ServiceImpl<CalcinationDesulfurizationDenitrationMapper, CalcinationDesulfurizationDenitration> implements ICalcinationDesulfurizationDenitrationService {


    @Autowired
    private CalcinationDesulfurizationDenitrationMapper calcinationDesulfurizationDenitrationMapper;

    @Autowired
    private IHktsTailGasService iHktsTailGasService;

    @Override
    public IPage<CalcinationDesulfurizationDenitration> selectPage(Page<CalcinationDesulfurizationDenitration> page, CalcinationDesulfurizationDenitration calcinationDesulfurizationDenitration) {
        QueryWrapper<CalcinationDesulfurizationDenitration> query = Wrappers.query();
        QueryWrapper<CalcinationDesulfurizationDenitration> entityWrapper = getEntityWrapper(query, calcinationDesulfurizationDenitration);
        return calcinationDesulfurizationDenitrationMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<CalcinationDesulfurizationDenitration> selectList(CalcinationDesulfurizationDenitration calcinationDesulfurizationDenitration) {
        QueryWrapper<CalcinationDesulfurizationDenitration> query = Wrappers.query();
        QueryWrapper<CalcinationDesulfurizationDenitration> entityWrapper = getEntityWrapper(query, calcinationDesulfurizationDenitration);
        return calcinationDesulfurizationDenitrationMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<CalcinationDesulfurizationDenitration> getEntityWrapper(QueryWrapper<CalcinationDesulfurizationDenitration> entityWrapper, CalcinationDesulfurizationDenitration calcinationDesulfurizationDenitration) {
        //条件拼接
        return entityWrapper;
    }
    @Override
    public IPage<CalcinationDesulfurizationDenitration> selectPageByTime(Page<CalcinationDesulfurizationDenitration> page, String startDate, String endDate){
        QueryWrapper<CalcinationDesulfurizationDenitration> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= '" + startDate + "'")
                .apply(!"".equals(endDate),
                        "time <= '" + endDate + "'")
        .orderByDesc(CalcinationDesulfurizationDenitration::getTime);
        IPage<CalcinationDesulfurizationDenitration> hktsTailGasPage = calcinationDesulfurizationDenitrationMapper.selectPage(page, query);
        return hktsTailGasPage;
    }

    @Override
    public void dataHandler() {
        HktsTailGas gas=new HktsTailGas();
        List<HktsTailGas> hktsTailGases = iHktsTailGasService.selectList(gas);
        CalcinationDesulfurizationDenitration Denitration=new CalcinationDesulfurizationDenitration();
        List<CalcinationDesulfurizationDenitration> calcinationDesulfurizationDenitrations = this.selectList(Denitration);
        for(int i=0;i<hktsTailGases.size();i++){
            CalcinationDesulfurizationDenitration calcinationDesulfurizationDenitration = calcinationDesulfurizationDenitrations.get(i);
            if(calcinationDesulfurizationDenitration!=null){
                calcinationDesulfurizationDenitration.setDataO2(hktsTailGases.get(i).getA19001Avg());
                calcinationDesulfurizationDenitration.setDataNox(hktsTailGases.get(i).getA21002Avg());
                calcinationDesulfurizationDenitration.setDataSo2(hktsTailGases.get(i).getA21026Avg());
                calcinationDesulfurizationDenitration.setDataFc(hktsTailGases.get(i).getA34013Avg());
                calcinationDesulfurizationDenitrationMapper.updateById(calcinationDesulfurizationDenitration);
            } else{
                break;
            }
        }


    }

    @Override
    public IPage<CalcinationDesulfurizationDenitration> getBrokenLineGraph(Page<CalcinationDesulfurizationDenitration> page, String startDate, String endDate){
        QueryWrapper<CalcinationDesulfurizationDenitration> query = Wrappers.query();
        query.lambda().apply(!"".equals(startDate),
                "time >= ' " + startDate + " ' " )
                .apply(!"".equals(endDate),
                        "time <= ' " + endDate + " ' ")
                .orderByAsc(CalcinationDesulfurizationDenitration::getTime);
        IPage calcinationDesulfurizationDenitrations = calcinationDesulfurizationDenitrationMapper.selectPage(page,query);
        List<CalcinationDesulfurizationDenitration> records = calcinationDesulfurizationDenitrations.getRecords();
        List<Map<String,Object>> resultDate = records.stream().map(t -> assemblyCargoData(t)).collect(Collectors.toList());
        Map<String, List<Object>> stringListMap = resultMapUtils.resultData(resultDate);
        List<Object> list1 = resultMapUtils.getList(resultDate);
        List<Object> listName = resultMapUtils.getListName(resultDate);
        Map<String,Object> data = new HashMap<>();
        data.put("list",listName);
        data.put("records",stringListMap);
        data.put("times",list1);
        List<Object> resultList = new ArrayList<>();
        resultList.add(data);
        IPage iPage = calcinationDesulfurizationDenitrations.setRecords(resultList);
        return iPage;
    }

    public Map<String,Object> assemblyCargoData(CalcinationDesulfurizationDenitration cDD){
        Map<String,Object> data = new LinkedHashMap<>();
        /*data.put("烟尘实测浓度(mg/m³)",cDD.getDataFc());
        data.put("氮氧化物实测浓度(mg/m³)",cDD.getDataNox());
        data.put("氧含量平均值(%)",cDD.getDataO2());
        data.put("二氧化硫实测浓度(mg/m³)",cDD.getDataSo2());
        data.put("1#装置氨气流量(nm3/h)",cDD.getDsjhFt101());
        data.put("2#装置氨气流量(nm3/h)",cDD.getDsjhFt102());*/
        data.put("氨气流量(nm3/h)",cDD.getDsjhFt());
        data.put("1#装置进口压力(Mpa)",cDD.getDsjhP111());
        data.put("2#装置进口压力(Mpa)",cDD.getDsjhP121());
        data.put("1#装置进口温度(℃)",cDD.getDsjhT111());
        data.put("2#装置进口温度(℃)",cDD.getDsjhT121());
        data.put("浆液池高度(m)",cDD.getDsjhTllt());
        data.put("脱硫塔进口温度(℃)",cDD.getDsjhTlmd());
        data.put("浆液酸碱度(kg/m3)",cDD.getDsjhTlph());
        data.put("time",cDD.getTime());
        return data;
    }
    /** 功能描述:
    *数据导出
    * @return:
    * @Author: 杨春科
    * @Date: 2021-06-09 15:47
    * @Description //TODO
    */
    @Override
    public void exportData(HttpServletRequest request, HttpServletResponse response) {
      //  String date = request.getParameter("date").toString();
        CalcinationDesulfurizationDenitration  Denitration=new CalcinationDesulfurizationDenitration();
        List<CalcinationDesulfurizationDenitration> calcinationDesulfurizationDenitrations = this.selectList(Denitration);
        List<Map<String,Object>> list=calcinationDesulfurizationDenitrations.stream().map(t-> JSONObject.parseObject(JSON.toJSONStringWithDateFormat(t,"yyyy-MM-dd HH:mm:ss"))).collect(Collectors.toList());
        /** 功能描述:
        *[request, response]
        * @return: void
        * @Author: 杨春科
        * @Date: 2021-06-09 16:33
        * @Description //TODO
        */
        List<String> listHead=new ArrayList<>();
        listHead.add("时间");listHead.add("浆液池高度");listHead.add("浆液密度");listHead.add("浆液密度");
        listHead.add("1#装置氨气流量");listHead.add("2#装置氨气流量");listHead.add("1#装置进口压力");
        listHead.add("2#装置进口压力");listHead.add("1#装置进口温度");listHead.add("2#装置进口温度");
        listHead.add("SO2实测浓度(二氧化硫)");listHead.add("NOX实测浓度(氮氧化物)");listHead.add("O2平均值(氧含量)");
        listHead.add("FC实测浓度(烟尘)");listHead.add("1#装置出口温度");listHead.add("2#装置出口温度");
        List<String> listHeadIndex=new ArrayList<>();
        listHeadIndex.add("time");listHeadIndex.add("dsjhTllt");listHeadIndex.add("dsjhTlmd");listHeadIndex.add("dsjhTlph");
        listHeadIndex.add("dsjhFt101");listHeadIndex.add("dsjhFt102");listHeadIndex.add("dsjhP111");
        listHeadIndex.add("dsjhP121");listHeadIndex.add("dsjhT111");listHeadIndex.add("dsjhT121");
        listHeadIndex.add("dataSo2");listHeadIndex.add("dataNox");listHeadIndex.add("dataO2");
        listHeadIndex.add("dataFc");listHeadIndex.add("dsjhT112");listHeadIndex.add("dsjhT122");
        ExcelPortUtil.excelPort("焙烧脱硫",listHead,listHeadIndex,list,null,response);

    }
    @Override
    public void importByFiles(List<CalcinationDesulfurizationDenitration> list) {
        this.saveBatch(list);
    }
    @Override
    public CalcinationDesulfurizationDenitration getDataByTime(String time) {
        return calcinationDesulfurizationDenitrationMapper.getDataByTime(time);
    }
    @Override
    public void deleteAll() {
        calcinationDesulfurizationDenitrationMapper.deleteAll();
    }

    @Override
    public void handlerData519() throws ParseException {
        QueryWrapper<CalcinationDesulfurizationDenitration> query = Wrappers.query();
        query.le(CalcinationDesulfurizationDenitration.TIME,"2021-05-19 01:12:00");
        List<CalcinationDesulfurizationDenitration> calcinationDesulfurizationDenitrations = calcinationDesulfurizationDenitrationMapper.selectList(query);
        query.clear();
        query.ge(CalcinationDesulfurizationDenitration.TIME,"2021-05-19 01:12:00");
        query.le(CalcinationDesulfurizationDenitration.TIME,"2021-06-01 00:00:00");
        List<CalcinationDesulfurizationDenitration> calcinationDesulfurizationDenitrations1 = calcinationDesulfurizationDenitrationMapper.selectList(query);
        for(int i=0;i<calcinationDesulfurizationDenitrations.size();i++){
          int yushu=  i%calcinationDesulfurizationDenitrations1.size();
            calcinationDesulfurizationDenitrations.get(i).setDsjhTllt(calcinationDesulfurizationDenitrations1.get(yushu).getDsjhTllt());
            calcinationDesulfurizationDenitrations.get(i).setDsjhTlph(DataHandlerUtils.electricDataHandler(calcinationDesulfurizationDenitrations1.get(yushu).getDsjhTlph())  );
            calcinationDesulfurizationDenitrations.get(i).setDsjhTlmd(DataHandlerUtils.electricDataHandler(calcinationDesulfurizationDenitrations1.get(yushu).getDsjhTlph()));
            calcinationDesulfurizationDenitrations.get(i).setDsjhFt101(DataHandlerUtils.electricDataHandler(calcinationDesulfurizationDenitrations1.get(yushu).getDsjhFt101()));
            calcinationDesulfurizationDenitrations.get(i).setDsjhFt102(DataHandlerUtils.electricDataHandler(calcinationDesulfurizationDenitrations1.get(yushu).getDsjhFt102()));
            calcinationDesulfurizationDenitrations.get(i).setDsjhP111(calcinationDesulfurizationDenitrations1.get(yushu).getDsjhP111());
            calcinationDesulfurizationDenitrations.get(i).setDsjhP121(DataHandlerUtils.electricDataHandler(calcinationDesulfurizationDenitrations1.get(yushu).getDsjhP121()));
        }
      this.saveOrUpdateBatch(calcinationDesulfurizationDenitrations);
    }

    @Override
    public void handlerTimeDifferenceData(String format, String format1) {
        calcinationDesulfurizationDenitrationMapper.updateByTime(format,format1);
    }

    @Override
    public void updateFtByTime() {
        calcinationDesulfurizationDenitrationMapper.updateFtByTime();
    }

    @Override
    public void deleteFtByTime() {
        calcinationDesulfurizationDenitrationMapper.deleteFtByTime();
    }

}
